package org.hawk.gwt.ppc.reflect;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;

import junit.framework.TestCase;

import org.hawk.gwt.ppc.javacc.JavaParserTreeConstants;
import org.hawk.gwt.ppc.javacc.JavaScanner;
import org.hawk.gwt.ppc.javacc.ParseException;
import org.hawk.gwt.ppc.scanner.UniversalSourcesLoader;
import org.hawk.gwt.ppc.utils.IO;

public class ClassDelegateBuilderTestCase extends TestCase {
	private static final String MOCK05 = "./src/org/hawk/gwt/ppc/reflect/mock/Mock5.java";
	private static final String MOCK05_PROCESSED = "./src/org/hawk/gwt/ppc/reflect/lang/mock/Mock1.java";
	private static final String MOCK06 = "./src/org/hawk/gwt/ppc/reflect/mock/Mock6.java";
	private static final String MOCK06_PROCESSED = "./src/org/hawk/gwt/ppc/reflect/lang/mock/Mock2.java";
	private static final String MOCK07 = "./src/org/hawk/gwt/ppc/reflect/mock/Mock7.java";
	private static final String MOCK07_PROCESSED = "./src/org/hawk/gwt/ppc/reflect/lang/mock/Mock3.java";
	
	public void testBuildClassDelegate() throws IOException, ParseException{
		assertEquals(getMock(MOCK05_PROCESSED), scan(MOCK05));
		assertEquals(getMock(MOCK06_PROCESSED), scan(MOCK06));
		assertEquals(getMock(MOCK07_PROCESSED), scan(MOCK07));
	}
	
	private String scan(String mock) throws ParseException,
			IOException {
		StringWriter stream = new StringWriter();
		MetadataProcessor processor = new MetadataProcessor(new UniversalSourcesLoader());
		JavaScanner scanner = new JavaScanner();
		scanner.addProcessor(JavaParserTreeConstants.JJTCOMPILATIONUNIT,
				processor);
		scanner.addProcessor(JavaParserTreeConstants.JJTCLASSORINTERFACEBODY,
				processor);
		scanner.addProcessor(JavaParserTreeConstants.JJTEXTENDSLIST,
				processor);
		scanner.addProcessor(JavaParserTreeConstants.JJTPACKAGEDECLARATION,
				processor);
		scanner.addProcessor(JavaParserTreeConstants.JJTIMPLEMENTSLIST,
				processor);
		scanner.addProcessor(JavaParserTreeConstants.JJTIMPORTDECLARATION,
				processor);
		
		scanner.startScan(new FileReader(mock),stream);
		return stream.toString().replaceAll("\r", "");
	}
	
	private String getMock(String mock) throws IOException{
		return IO.toString(new FileReader(mock))
			.replaceAll("org.hawk.gwt.ppc.reflect.lang.mock", "org.hawk.gwt.ppc.reflect.mock")
			.replaceAll("Mock1", "Mock5")
			.replaceAll("Mock2", "Mock6")
			.replaceAll("Mock3", "Mock7")
			.replaceAll("\r", "");
	}
}
